<template>
  <BasicModal
    v-bind="$attrs"
    :width="modalWidth"
    @register="registerModal"
    title="绑定授权码"
    @ok="handleOK"
  >
    <!-- :labelWidth="100"
      :schemas="schemas"
      :actionColOptions="{ span: 24 }"
      @submit="handleSubmit"
      :showActionButtonGroup="showActionButtonGroup"
      v-if="current === 0" -->
    <BasicForm @register="registerForm" />
    <!-- :labelWidth="100"
      :schemas="schemasData"
      :actionColOptions="{ span: 24 }"
      :baseColProps="{ span: 24 }"
      @submit="handleSubmit"
      :showActionButtonGroup="showActionButtonGroup"
      v-if="current === 1" -->
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { bindAuthorizationCode } from '@/api/demo/device';
  import { accountFormSchema } from './account.data';
  defineOptions({ name: 'AccountModal' });
  // const emit = defineEmits(['success', 'register']);
  const isUpdate = ref(true);
  const rowId = ref('');
  const modalWidth = ref('25%'); // 可以是像 600 这样的像素值，或者百分比 '50%'
  const emit = defineEmits(['success']);
  //注册表格数据
  const [registerForm, { resetFields, setFieldsValue, updateSchema, getFieldsValue }] = useForm({
    labelWidth: 90,
    baseColProps: { span: 24 },
    schemas: accountFormSchema,
    showActionButtonGroup: false,
  });
  //注册弹窗数据
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      rowId.value = data.record.id;
      setFieldsValue({
        ...data.record,
      });
    }
  });
  async function handleOK() {
    try {
      let data = await bindAuthorizationCode({ ...getFieldsValue() });
      emit('success', data); // 触发 success 事件
      closeModal();
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
